package com.sskj.fca.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.constraint.ConstraintLayout;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.util.QRCodeUtil;
import com.sskj.fca.R;
import com.sskj.fca.bean.CommonBean;
import com.sskj.fca.presenter.DownloadFragmentPresenter;

import butterknife.BindView;

/**
 * Created by QinKang on 2018/12/29.
 */

@Route(path = RConfig.APP_FRAGMENT_APP_DOWMLOAD)
public class DownloadFragment extends BaseFragment<DownloadFragmentPresenter> {
    @BindView(R.id.download_apple)
    ConstraintLayout downloadApple;
    @BindView(R.id.download_android)
    ConstraintLayout downloadAndroid;
    @BindView(R.id.download_QRCode)
    ImageView downloadQRCode;

    @Override
    protected int getLayoutId() {
        return R.layout.app_fragment_download;
    }

    @Override
    protected DownloadFragmentPresenter getPresenter() {
        return new DownloadFragmentPresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getCommonData();
        }
    }

    @Override
    public void initView() {
        mPresenter.getCommonData();
    }

    public void updataUI(CommonBean commonBean) {
        String app_down_url = commonBean.getApp_down_url();
//        ImageUtil.setImage(BaseHttpConfig.BASE_URL + app_down_url, downloadQRCode);

        ClickUtil.click(downloadApple, () -> {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(BaseHttpConfig.DOWNLOAD_URL);
            intent.setData(content_url);
            startActivity(intent);
        });

        QRCodeUtil.createImage(BaseHttpConfig.DOWNLOAD_URL, (int) RudenessScreenHelper.pt2px(getActivity(), 230), new QRCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                downloadQRCode.setImageBitmap(bitmap);
            }

            @Override
            public void onAnalyzeFailed() {

            }
        });

        ClickUtil.click(downloadAndroid, () -> {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(BaseHttpConfig.DOWNLOAD_URL);
            intent.setData(content_url);
            startActivity(intent);
        });
    }
}
